package br.com.post.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailUtil {
	
	public static String HOST="smtp.locaweb.com.br";
	public static String EMAIL_HOST="smtp2@locaweb.com.br";
	public static String SMTP_USER="user";
	public static String SMTP_PASSWORD="password";
	
	/**
	 * Envia um email simples, texto e html
	 * @param recipient, Email do Destinatario
	 * @param nameRecipient, Nome do Destinatario
	 * @param from, Email do Rementente
	 * @param nameFrom, Nome do Remetente
	 * @param message, Mensagem a ser enviada
	 * @param subject, Assunto da mensagem
	 * @throws Exception
	 */
	public static void sendEmail(String recipient, String nameRecipient, String from, String nameFrom, String message, String subject ) throws Exception {
		
		Properties mailprop = new Properties();
		mailprop.put("mail.host", EMAIL_HOST);   
		mailprop.put("mail.smtp.host", HOST);  
		mailprop.put("mail.store.protocol", "pop3");  
		mailprop.put("mail.transport.protocol", "smtp");  
		mailprop.put("mail.smtp.auth", "true");  
		mailprop.put("mail.smtp.port", "25");   
		
        Authenticator auth = new Authenticator(){
        	 @Override
             public PasswordAuthentication getPasswordAuthentication() {  
                 return new PasswordAuthentication(SMTP_USER, SMTP_PASSWORD);  
            }  
        }; 
        
		Session session = Session.getInstance(mailprop, auth);
		MimeMessage msg = new MimeMessage(session);

		//Remetente
		InternetAddress internetAddress = new InternetAddress(from);
		internetAddress.setPersonal(nameFrom);
		msg.setFrom(internetAddress);
		//Destinatario
		internetAddress = new InternetAddress(recipient);
		internetAddress.setPersonal(nameRecipient);
		msg.setRecipient(Message.RecipientType.TO, internetAddress);
		//data do envio
		msg.setSentDate(new Date());
		// assunto
		msg.setSubject(subject);
		// conteudo da mensagem
		msg.setContent(message,"text/html");
		 
		msg.saveChanges();
		// enviar email
		Transport.send(msg);
			
	}
}
